<?php

use Biz\service\Album\AlbumListService;
use Biz\service\Album\FormatAlbumService;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\service\User;
use Doraemon\tools\BlockType;

class DrawerAlbumListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;
    const PAGE_SIZE = 10;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currUid = $this->uid;
        $uid = (int)trim($this->getRequest()->getPost('uid', 0));
        $page = $this->getRequest()->getPost('page', 1);
        $pageSize = $this->getRequest()->getPost('page_size', self::PAGE_SIZE);

        if ($uid == 0) { //如果没有uid参数 则uid默认为当前登录用户uid
            $uid = $currUid;
        } else {
            $userInfo = User::getUser($uid);
            if (!$userInfo) {
                throw new Exception("用户不存在", self::CODE_ERROR_VIEW_TOSTA);
            }
        }
        $result = [];
        list($list, $total, $isOver) = AlbumListService::getSelfBuiltList($uid, $currUid, $page, $pageSize);

        if ($list && is_array($list)) {
            $formatInfo = FormatAlbumService::_formatAlbumList($list, FormatAlbumService::DRAWER_ALBUM_LIST);
            foreach ($formatInfo as $key => $val) {
               $val['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_SELECT_ALBUM_ALBUM_LIST, [
                   basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI_ALBUM,
                   basicStatSign::EXTEND_KEY_OBJ_ID => $val['album_info']['album_id'],
               ]);
                $result['list'][] = $val;
            }
        }

        $result['total'] = $total;
        $result['is_over'] = $isOver;

        $this->setSuccess($result, self::API_CODE_SUCCES);

    }
}